<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\ChangeRepository;
use Illuminate\Http\Request;

/**
 * 退单管理控制器
 *
 * @package App\Http\Controllers\Seller
 */
class ChangeController extends BaseController
{
    /**
     * @var ChangeRepository
     */
    protected $change_repo;

    public function __construct(ChangeRepository $change_repo,Request $request)
    {
        parent::__construct();

        $this->change_repo = $change_repo;
        $this->request = $request;
        
        $this->shop_id = array_get($this->user, 'shop_id');
        //用户只能操作自身所属数据
        $this->change_repo->scopeQuery(function ($query) {
            // 条件组合
            return $query->where('shop_id', $this->shop_id);
        });

    }

    public function index()
    {
        // $this->log(var_export($this->request->all(),1),'changeIndex');
        $change_repo = $this->change_repo->paginate($this->perPage);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $change_repo,true);
    }

    public function show($id)
    {
        $order = $this->change_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $order['data']);
    }

    public function store(Request $request)
    {
        //卖家无创建
        $this->validate($request, [
            'order_status' => 'required'
        ]);
        $data = $request->all();

        $ret = $this->change_repo->update($data);

        return $this->noDataReturn($ret ? true : false);
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'status' => 'required',
            'change_id' => 'required',
            'shop_id' => 'required'
        ]);
        $data = $request->all();
        $change_order = $this->change_repo->find($data['change_id']);
        //判断退货退款
        if($change_order['data']['change_type'] == 0 && ($data['status'] == 1||$data['status'] == 3)){
            $ret =true ;
        }elseif($change_order['data']['change_type'] == 1 && ($data['status'] == 2||$data['status'] == 3)){
            $ret =true ;
        }else{
            return $this->noDataReturn( false);
        }

        //当状态修改为确认收货，或者同意退款时需要退钱，
        if($change_order['data']['change_type'] == 0 || ($change_order['data']['change_type'] == 1 && $change_order['data']['status'] == 1) ){
            //调用李杰付款方法
        }

        $updata=[
            'status'=>$data['status']
        ];
        $ret = $this->change_repo->update($updata, $data['change_id']);

        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id)
    {
        //用户无删除订单操作，以下作为样例
        $ret = $this->orders_repo->delete($id);

        return $this->noDataReturn($ret ? true : false);
    }
    public function log($data,$filename){
        if(empty($filename)){
            $log_filename='C:\Users\admin\Desktop\ecclub.php';
        }
        else{
            $log_filename='C:\Users\admin\Desktop\\'.$filename.'.php';
        }
        // 如果文件夹不存在则创建文件夹
        is_dir(dirname($log_filename)) || mkdir(dirname($log_filename),0777,true);
        // 日志大小
        $max_size=10485760;
        // 判断文件大小做自动删除动作
        if(file_exists($log_filename) && abs(filesize($log_filename))>$max_size){
            // 删除文件
            unlink($log_filename);
        }else{
            // 写日志(第三个参数是系统函数，可以连续写文件不覆盖)
            file_put_contents($log_filename, date("Y-m-d H:i:s")." ".$data."\n", FILE_APPEND);
        }
    }
}
